package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        if (setmealPageQueryDTO.getPage() == 0) {
            setmealPageQueryDTO.setPage(1);
        }
        if (setmealPageQueryDTO.getPageSize() == 0) {
            setmealPageQueryDTO.setPageSize(5);
        }

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOPage = setmealMapper.page(setmealPageQueryDTO);
        if (setmealVOPage.size() > 0 && setmealVOPage != null) {
            for (SetmealVO setmealVO : setmealVOPage) {
                Category category = categoryMapper.getById(setmealVO.getCategoryId());
                setmealVO.setCategoryName(category.getName());
                List<SetmealDish> setmealDishes = setmealDishMapper.getById(setmealVO.getId());
                setmealVO.setSetmealDishes(setmealDishes);
            }

        }

        return Result.success(new PageResult(setmealVOPage.getTotal(), setmealVOPage.getResult()));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Setmeal setmealMapperById = setmealMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmealMapperById, setmealVO);
        List<SetmealDish> setmealDishList = setmealDishMapper.getById(setmealVO.getId());
        if (setmealDishList.size() > 0 && setmealDishList != null) {
            setmealVO.setSetmealDishes(setmealDishList);
        }
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public Result update(SetmealDTO dto) {
        if (dto.getId() == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealId(dto.getId());
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes.size() > 0 && setmealDishes != null) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(dto.getId()));
            setmealDishMapper.add(setmealDishes);

        }
        return Result.success();
    }

    /**
     * 启售禁售
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result status(Integer status, Long id) {
        if (status == null || id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //判断套餐中是否包含停售菜品如果有就不能启用 TODO

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Result delete(List<Long> ids) {
        if (ids == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        for (Long id : ids) {
            Setmeal bySetmealId = setmealMapper.getBySetmealId(id);
            if (bySetmealId != null) {
                if (bySetmealId.getStatus() == StatusConstant.DISABLE) {
                    setmealMapper.delete(id);
                    setmealDishMapper.deleteBySetmealId(id);
                } else {
                    return Result.error(MessageConstant.SETMEAL_ON_SALE);
                }
            }
        }
        return Result.success();
    }

    /**
     * 新增套餐
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public Result add(SetmealDTO dto) {
        if (dto.getName() == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Setmeal setmeal = setmealMapper.getByName(dto.getName());
        if (setmeal != null) {
            return Result.error(MessageConstant.DISHNAME_EXIT_FOUND);
        }
        Setmeal NnewSetmeal = new Setmeal();
        BeanUtils.copyProperties(dto, NnewSetmeal);
        setmealMapper.add(NnewSetmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(NnewSetmeal.getId()));
            setmealDishMapper.add(setmealDishes);
        }
        return Result.success();
    }

    /*----------------------------c端-------------------------*/




    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
